package com.sengled.cloud.biz.union.mgt.api.config;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by ti on 2017/10/9.
 */
@Configuration
public class SengledProperties {
    private static Logger logger = LoggerFactory.getLogger(SengledProperties.class);

    private Properties properties;
    /**
     * 配置文件列表
     */
    private Map<String, Properties> propertiesMap = null;

    public SengledProperties(@Value("${default.properties.file.path}") String filePath) {
        properties = getProperties(filePath);
        propertiesMap = new HashMap<String, Properties>();
    }

    /**
     * 获取属性配置文件
     *
     * @param fileName
     * @return
     * @autor:congge
     * @createDate:2015-4-30上午11:27:13
     */
    public Properties getProperties(String fileName) {
        Properties result = new Properties();
        try {
            InputStream is = FileUtils.openInputStream(new File(fileName));
            result.load(is);
        } catch (FileNotFoundException e) {
            logger.error("error occured:CommonDefaultPropertiesUtil->getProperties", e);
            // e.printStackTrace();
        } catch (IOException e) {
            logger.error("error occured:CommonDefaultPropertiesUtil->getProperties", e);
            // e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取字符串类型的属性
     *
     * @param name 属性名
     * @return
     */
    public String getProperty(String name) {
        return properties.getProperty(name);
    }

    /**
     * 根据配置文件名称和配置文件key获取设定值
     *
     * @param key            配置文件key名字
     * @param propertiesName 格式:xxx.properties
     * @description
     * @autor:congge
     * @createDate:2015-4-30上午11:37:41
     */
    public String getProperty(String key, String propertiesName) {
        Properties properties = propertiesMap.get(propertiesName);
        // 如果配置文件列表里没有该配置文件，将其添加到列表
        if (null == properties) {
            try {
                properties = new Properties();
                InputStream is = SengledProperties.class
                        .getResourceAsStream(propertiesName);
                if (null == is) {
                    return null;
                }
                if (is.available() < 0) {
                    return null;
                }
                properties.load(is);
                propertiesMap.put(propertiesName, properties);
                return properties.getProperty(key);
            } catch (IOException e) {
                logger.error("error occured:PropertiesUtil->getProperty", e);
            }
        }
        return properties.getProperty(key);
    }
}
